﻿// <copyright file="admin_Gallary.aspx.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Net.Ntalent.Action50.Core.Controls;
using Net.Ntalent.Action50.Core.Dao;
using Net.Ntalent.Action50.Core.Service;
using Net.Ntalent.Action50.Core.Util;
using Net.Ntalent.Action50.DataAccess;

/// <summary>
/// 管理页面，浏览某个action的gallery，并可以上传图片
/// </summary>
///<author>王彦博</author> 
/// <copyright>
/// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
/// </copyright>
/// <version>1.0.0</version>
public partial class admin_Gallary : PageBase
{
    public admin_Gallary()
    {
        //设置权限，使未登录用户不能访问该页
        authentication = Constants.AUTHENTICATION_ADMIN;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        GallaryDao gallaryDao = new GallaryDao();

        //执行删除操作
        if (e.CommandName == "deleteGallary")
        {
            T_GALLARY gallary = gallaryDao.Retrieve(Convert.ToInt64(e.CommandArgument));
            T_ACTIONS action = new ActionDao().Retrieve(gallary.ACTION_ID); 

            if (gallary.Id == action.LOGO)
            {
                //如果欲删除的图片是logo，弹出窗口提示提logo不能删除
                Message.Text = "Logo Couldn't Be Deleted!";
                popupCmd.Text =
                    "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
                return;
            }

            try
            {
                gallaryDao.Delete(Convert.ToInt64(gallary.Id));
                //弹出窗口提示删除成功
                Message.Text = "Delete Successfully！";
                popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            catch
            {
                //弹出窗口提示删除失败
                Message.Text = "Delete Failed！";
                popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            finally
            {
                ListView1.DataBind();
            }
        }

        //执行更新操作
        if (e.CommandName == "submitGallary")
        {
            long gallaryId = Convert.ToInt64(e.CommandArgument);
            TextBox tb = (TextBox)(((LinkButton)(e.CommandSource)).Parent.FindControl("TextBox1"));
            T_GALLARY gallary = gallaryDao.Retrieve(gallaryId);

            gallary.GALLARY_DESC = tb.Text.Trim();
            try
            {
                gallaryDao.Update(gallary);
                //弹出窗口提示更新成功
                Message.Text = "Submit Successfully！";
                popupCmd.Text =
                    "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            catch
            {
                //弹出窗口提示更新失败
                Message.Text = "Submit Failed！";
                popupCmd.Text =
                    "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            finally
            {
                ListView1.DataBind();
            }
        }

        //把某张图片设置为Landscape
        if (e.CommandName == "setCover")
        {
            long actionId = Convert.ToInt64(Helper.getQueryString(Context, "actionId"));
            IList<T_GALLARY> gallarys = gallaryDao.RetrieveByAction(actionId, GallaryType.LANDSCAPE, 0, 0, string.Empty);

            //将要被设为landscape的图片
            T_GALLARY gallary = gallaryDao.Retrieve(Convert.ToInt64(e.CommandArgument));

            if (gallarys.Count < 1)//该action尚无landscape
            {
                gallary.GALLARY_TYPE = (int)GallaryType.LANDSCAPE;
                gallaryDao.Update(gallary);
            }
            else//该action已经有了landscape
            {
                //先将原有的landscape类型更改为normal
                foreach (var landscape in gallarys)
                {
                    landscape.GALLARY_TYPE = (int)GallaryType.NORMAL;
                    gallaryDao.Update(landscape);
                }

                //将该图类型设置成landscape
                gallary.GALLARY_TYPE = (int)GallaryType.LANDSCAPE;
                gallaryDao.Update(gallary);
            }

            //弹出提示，设置成功
            Message.Text = "Set Cover Successfully！";
            popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
        }
    }

    protected void LinkButton_Upload_Click(object sender, EventArgs e)
    {
    }

    /// <summary>
    /// Logout
    /// </summary>
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        AuthenticationService authenticationService = new AuthenticationService();
        authenticationService.LogOut();
        Response.Redirect(Request.UrlReferrer.ToString());
    }

    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        //如果数据不足够分页，则隐藏分页控件
        //如果数据不够一页，则不显示分页控件。
        long actionId = Convert.ToInt64(Request.QueryString["actionid"]);
        var gallaryDao = new GallaryDao();
        long gallaryCount = gallaryDao.RetrieveByAction(actionId, GallaryType.ALL, 0, 0, "id").Count();
        if (gallaryCount <= 12)
        {
            DataPager1.Visible = false;
        }
    }
}
